<!-- 疾病管理 -->
<template>
  <div class="app-container" style="padding-top: 5px">
    <el-tabs v-model="queryParams.syndromeType" @tab-click="handleQuery">
      <el-tab-pane label="中医疾病辨证" name="0"></el-tab-pane>
      <el-tab-pane label="贴敷疾病辨证" name="1"></el-tab-pane>
    </el-tabs>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="编码/西医/中医病名/首字母"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 250px"
        />
      </el-form-item>
      <el-form-item
        v-if="queryParams.syndromeType == 1"
        label="适用范围"
        prop="regionCode"
        label-width="130"
      >
        <el-select
          v-model="queryParams.regionCode"
          placeholder="请选择省份"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="item in areaList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          v-hasPermi="['saas:illness:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="openImport()"
          >批量导入</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        label="西医疾病编码"
        align="center"
        prop="diseaseCode"
        width="150"
      />
      <el-table-column label="西医病名" align="center" prop="diseaseName" />
      <el-table-column
        label="疾病首字母"
        align="center"
        prop="diseaseFirstLetter"
        width="120"
      />
      <el-table-column label="中医病名" align="center" prop="tcmDiseaseName" />
      <el-table-column label="疾病说明" align="center">
        <template slot-scope="{ row }">
          <div class="ov-2">
            {{ row.diseaseNote }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="辨证路径" align="center" width="100">
        <template slot-scope="{ row }">
          <span v-if="row.syndromeType === 0">中医辨证</span>
          <span v-if="row.syndromeType === 1">贴敷辨证</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.syndromeType == 1" label="适用范围" align="center" width="100">
        <template slot-scope="{ row }">
          {{ row.regionName }}
        </template>
      </el-table-column>

      <el-table-column label="疾病标签" align="center" width="220">
        <template slot-scope="{ row }">
          <TableTagList
            :tagList="row.tags"
            closable
            @close="(tag) => handleDeleteTag(row, tag)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="虚拟辩证次数"
        align="center"
        prop="fictitiousCount"
        width="100"
      >
        <template slot-scope="{ row }">
          <span>{{ row.fictitiousCount || 0 }}</span>
          <el-popover
            placement="right"
            width="100"
            trigger="click"
            @show="setFictitiousCount(row.fictitiousCount)"
            @hide="editFictitiousCount(row)"
          >
            <el-input-number
              v-model="fictitiousCount"
              controls-position="right"
              step-strictly
              :min="0"
              style="max-width: 120px"
            ></el-input-number>
            <i
              slot="reference"
              class="iconfont el-icon-edit colorZS cup pa-10"
            ></i>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="实际辩证次数"
        align="center"
        prop="quoteCount"
        width="100"
      />
      <el-table-column label="总辩证次数" align="center" width="100">
        <template slot-scope="{ row }">
          {{ row.quoteCount + row.fictitiousCount }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="140">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            active-text="启用"
            :inactive-value="0"
            inactive-text="禁用"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button v-hasPermi="['saas:illness:edit']" size="mini" type="text" @click="handleOpen(scope.row.id)"
            >编辑</el-button
          >
          <el-button v-hasPermi="['saas:illness:remove']" size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
          <el-button v-hasPermi="['saas:illness:add']" size="mini" type="text" @click="copyList(scope.row.id)"
            >复制创建</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <ImportModal
      ref="ImportModal"
      title="导入疾病"
      url="/tool/importData/tcm/importDisease"
      templateUrl="https://onelive.obs.cn-south-1.myhuaweicloud.com/import-temp/%E7%96%BE%E7%97%85%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx"
      @refresh="resetQuery"
      @openErr="openErrorModal"
    />

    <el-dialog
      width="800px"
      title="本次导入数据部分失败，详情请查看失败原因"
      :visible.sync="errorVisible"
      @close="errorVisible = false"
    >
      <el-table :data="errorList">
        <el-table-column label="西医病名" align="center" prop="xname" />
        <el-table-column label="中医病名" align="center" prop="zname" />
        <el-table-column label="疾病标签" align="center" prop="tag" />
        <el-table-column label="导入失败原因" align="center" prop="reason" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDiseaseList,
  deleteDisease,
  updateDisease,
  setFictitious
} from "@/api/saas/tcm.js";
import TableTagList from "../tagList/tableTagList";
import ImportModal from "@/components/importModal";
import { listArea } from "@/api/saas/area";

export default {
  name: "Illness",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
        syndromeType: "0",
        regionCode: undefined
      },
      // 总条数
      total: 0,
      dataList: [],
      areaList: [], // 省份列表

      errorVisible: false,
      errorList: [],
      fictitiousCount: 0
    };
  },
  components: {
    TableTagList,
    ImportModal,
  },
  created() {
    this.getList();
    this.getAreaList();
  },
  activated() {
    this.getList();
    this.getAreaList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getDiseaseList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      const syndromeType = this.queryParams.syndromeType;
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.queryParams.syndromeType = syndromeType;
      this.getList();
    },
    // 获取省份列表
    getAreaList() {
      listArea({ level: 1 }).then(res => {
        this.areaList = res.data;
      })
    },
    handleOpen(id) {
      let query = id ? { id } : { syndromeType: this.queryParams.syndromeType };
      this.$router.push({ path: "/saas/editIllness/index", query });
    },
    copyList(id) {
      let query = { id, type: 'copy' };
      this.$router.push({ path: "/saas/editIllness/index", query });
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该疾病吗？")
        .then(function () {
          return deleteDisease(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 修改状态
    changeStatus(row) {
      updateDisease({ id: row.id, status: row.status })
        .then((res) => {
          this.$message.success("状态修改成功");
        })
        .catch((err) => {
          row.status = row.status ? 0 : 1; // 修改失败,复位
        });
    },
    // 刪除标签
    handleDeleteTag(row, tag) {
      const tags = row.tags.filter((v) => v.id !== tag.id);
      const diseaseTag = tags.map((v) => v.id);
      updateDisease({ id: row.id, diseaseTag: diseaseTag }).then((res) => {
        row.tags = tags;
      });
    },
    openImport() {
      this.$refs.ImportModal.openModal();
    },
    openErrorModal(list) {
      this.errorList = list;
      this.errorVisible = true;
    },
    setFictitiousCount(num) {
      this.fictitiousCount = num;
    },
    editFictitiousCount(row) {
      setFictitious({ id: row.id, fictitiousCount: this.fictitiousCount || 0 }).then(() => {
        row.fictitiousCount = this.fictitiousCount || 0;
        this.$message.success('修改成功')
      })
    }
  },
};
</script>
